package com.wolfalpha.utron.service.impl;

import android.text.TextUtils;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.Information;
import com.wolfalpha.utron.bean.InformationComment;
import com.wolfalpha.utron.bean.ItemInformation;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.InformationService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/8/20.
 */
public class InformationServiceImpl implements InformationService {


    @Override
    public List<ItemInformation> getList(int page) {
        List<ItemInformation> itemInformations = new ArrayList<ItemInformation>();
        if (page < 1) {
            page = 1;
        }
        String result = AppContext.getApiClient().sendGetResquest(URLs.INFORMATION_URL + "getList&page=" + String.valueOf(page));
        if (result == null) {
            return null;
        }
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemInformation information = JsonUtils.paraseJson(arr.get(i).toString(), ItemInformation.class);
                itemInformations.add(information);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return itemInformations;
    }

    @Override
    public Information getDetail(int id) {
        List<InformationComment> comments = new ArrayList<InformationComment>();
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(id));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "getDetail", mp);
        System.out.println(result);
        if (result == null) {
            return null;
        }
        return JsonUtils.paraseJson(JsonUtils.getData(result), Information.class);
    }

    @Override
    public boolean doComment(int infoId, String content) {
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(infoId));
        mp.put("to_id", "0");
        mp.put("content", content);
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "comment", mp);
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean doCommentToUser(int infoId, int toId, String content) {
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(infoId));
        mp.put("to_id", String.valueOf(toId));
        mp.put("content", content);
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));


        System.out.println(mp.get("content"));
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "comment", mp);
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean collect(int infoId, boolean op) {
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(infoId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        if (op) {
            mp.put("do", "add");
        } else {
            mp.put("do", "cancel");
        }
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "collect", mp);
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean praise(int infoId, boolean op) {
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(infoId));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        if (op) {
            mp.put("do", "praise");
        } else {
            mp.put("do", "dispraise");
        }
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "praise", mp);
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean share(int infoId) {
        Map<String, String> mp = new HashMap<>();
        mp.put("info_id", String.valueOf(infoId));
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "share", mp);
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }

    @Override
    public List<InformationComment> getComments(int infoId, int page) {
        List<InformationComment> comments = new ArrayList<InformationComment>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("info_id", infoId + "");
        String result = AppContext.getApiClient().sendPostResquest(URLs.INFORMATION_URL + "getComments", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                InformationComment comment = JsonUtils.paraseJson(arr.get(i).toString(), InformationComment.class);
                comments.add(comment);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return comments;
    }
}
